﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Inbox.Domain
{
    //[Table]
    //[InheritanceMapping(Code = "User", Type = typeof(PersistentObject), IsDefault = true)]
    //[InheritanceMapping(Code = "Role", Type = typeof(InboxUser))]
    public abstract class PersistentObject
    {
        #region **Properties**
        protected Guid _ID;
        protected bool _deleted;
        protected string _createUser;
        protected DateTime _createDate;
        protected Binary _timeStamp;

        [Column(Name = "ID", IsDiscriminator = true, Storage = "_ID", DbType = "UniqueIdentifier NOT NULL", IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
        public virtual Guid ID
        {
            get
            {
                return this._ID;
            }
            set
            {
                this._ID = value;
            }
        }

        [Column(Storage = "_deleted", DbType = "Bit", UpdateCheck = UpdateCheck.Never)]
        public virtual bool Deleted
        {
            get
            {
                return this._deleted;
            }
            set
            {
                this._deleted = value;
            }
        }

        [Column(Storage = "_createUser", DbType = "NVarChar(50)", UpdateCheck = UpdateCheck.Never)]
        public virtual string CreateUser
        {
            get
            {
                return this._createUser;
            }
            set
            {
                this._createUser = value;
            }
        }

        [Column(Storage = "_createDate", DbType = "DateTime", UpdateCheck = UpdateCheck.Never)]
        public virtual DateTime CreateDate
        {
            get
            {
                return this._createDate;
            }
            set
            {
                this._createDate = value;
            }
        }

        [Column(Storage = "_timeStamp", AutoSync = AutoSync.Always, DbType = "rowversion", IsDbGenerated = true, IsVersion = true, UpdateCheck = UpdateCheck.Never)]
        public virtual System.Data.Linq.Binary TimeStamp
        {
            get
            {
                return this._timeStamp;
            }
        }
        #endregion

        public void Insert()
        {
            using (InboxContext context = new InboxContext())
            {
                this._ID = Guid.NewGuid();
                context.InsertEntity(this);
            }
        }

        public bool Delete()
        {
            using (InboxContext context = new InboxContext())
            {
                return context.DeleteEntity(this);
            }

        }

        public bool Update()
        {
            using (InboxContext context = new InboxContext())
            {
                return context.UpdateEntity(this);
            }
        }
    }
}
